package com.itelecom.oss.mobile.services;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import android.util.Xml;
import android.view.InflateException;

import com.itelecom.oss.Constants;
//import com.itelecom.oss.mobile.domains.Location;

public class ParserServiceImpl implements ParserService, ResponseParser {
	
    @SuppressWarnings("unused")
	private void parseResponse(InputStream in, ResponseParser responseParser) throws IOException {
        final XmlPullParser parser = Xml.newPullParser();
        try {
            parser.setInput(new InputStreamReader(in));

            int type;
            while ((type = parser.next()) != XmlPullParser.START_TAG &&
                    type != XmlPullParser.END_DOCUMENT) {
                // Empty
            }

            if (type != XmlPullParser.START_TAG) {
                throw new InflateException(parser.getPositionDescription()
                        + ": No start tag found!");
            }

            String name = parser.getName();
            if (Constants.RESPONSE_TAG_RSP.equals(name)) {
                final String value = parser.getAttributeValue(null,Constants.RESPONSE_ATTR_STAT); 
                if(!Constants.RESPONSE_STATUS_OK.equals(value)) {
                    throw new IOException("Wrong status: " + value);
                }
            }

            responseParser.parseResponse(parser);

        } catch (XmlPullParserException e) {
            final IOException ioe = new IOException("Could not parser the response");
            ioe.initCause(e);
            throw ioe;
        }
    }


//	@SuppressWarnings("unused")
//	private void parseDeviceLocation(XmlPullParser parser, Location location)
//    throws XmlPullParserException, IOException {
//int type;
//String name;
//final int depth = parser.getDepth();
//
//while (((type = parser.next()) != XmlPullParser.END_TAG ||
//        parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
//    if (type != XmlPullParser.START_TAG) {
//        continue;
//    }
//
//    name = parser.getName();
//    if (Constants.RESPONSE_TAG_LOCATION.equals(name)) {
//        try {
//            location.setmLatitude(Float.parseFloat(parser.getAttributeValue(null,
//                    Constants.RESPONSE_ATTR_LATITUDE)));
//            location.setmLongitude(Float.parseFloat(parser.getAttributeValue(null,
//                    Constants.RESPONSE_ATTR_LONGITUDE)));
//        } catch (NumberFormatException e) {
//            throw new XmlPullParserException("Could not parse lat/lon", parser, e);
//        }
//    }
//}
//}

    @SuppressWarnings("unused")
	private void parseUser(XmlPullParser parser, String[] userId)
    throws XmlPullParserException, IOException {
int type;
String name;
final int depth = parser.getDepth();

while (((type = parser.next()) != XmlPullParser.END_TAG ||
        parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
    if (type != XmlPullParser.START_TAG) {
        continue;
    }

    name = parser.getName();
    if (Constants.RESPONSE_TAG_USER.equals(name)) {
        userId[0] = parser.getAttributeValue(null, Constants.RESPONSE_ATTR_NSID);
    }
}
}


	@Override
	public void parseResponse(XmlPullParser parser)
			throws XmlPullParserException, IOException {
		// TODO Auto-generated method stub
		
	}

}
